package com.lanxin.oa.approve.dao;

import com.lanxin.base.BaseDaoImpl;
import com.lanxin.bean.LcApproval;
import com.lanxin.bean.LcFlow;
import com.lanxin.bean.ZzEmployee;
import com.lanxin.util.PageBean;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Repository
public class FlowDaoImpl extends BaseDaoImpl implements FlowDao {
    @Override
    public PageBean selectMyFlow(LcApproval approval, HttpSession session, Integer page, Integer pageSize) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Integer empid = (Integer) session.getAttribute("empid");
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        String hql = ("from LcApproval a where a.proposer=" + empid);
        if (approval != null) {
            if (approval.getDocNo() != null && !"".equals(approval.getDocNo())) {
                hql = hql + " and a.docNo like'%" + approval.getDocNo() + "%'";
            }
            if (approval.getDocType() != null && !"".equals(approval.getDocType())) {
                hql = hql + " and a.docType='" + approval.getDocType() + "'";
            }
            if (approval.getStatus() != null && !"".equals(approval.getStatus())) {
                hql = hql + " and a.status='" + approval.getStatus() + "'";
            }
            if (approval.getSubmitDate() != null && !"".equals(approval.getSubmitDate())) {
                hql = hql + " and a.submitDate>='" + sim.format(approval.getSubmitDate()) + "'";
            }
            if (approval.getApprovalDate() != null && !"".equals(approval.getApprovalDate())) {
                hql = hql + " and a.submitDate<='" + sim.format(approval.getApprovalDate()) + "'";
            }
        }
        hql = hql + " and a.oid in(select max(oid) from LcApproval group by docNo)";
        hql = hql + "order by a.submitDate desc";
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public List<LcFlow> selectLcFlow() {
        return queryAll("from LcFlow");
    }

    @Override
    public List<LcApproval> selectApproveJiLu(String docNo) {
        return queryAll("from LcApproval l left join fetch l.node n left join fetch n.dept left join fetch n.job left join fetch l.dept left join fetch l.approvalEmp where l.docNo='" + docNo + "'");
    }

    @Override
    public PageBean selectApprovalById(LcApproval approval, Integer empid, Integer page, Integer pageSize) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        String empHQL = "from ZzEmployee e where e.oid=" + empid;
        ZzEmployee emp = (ZzEmployee)selectByHQL(empHQL);
        String hql = ("from LcApproval a left join fetch a.node n left join fetch a.dept d left join fetch a.emp e where a.status='S' " +
                "and ((n.isByDept='Y' and d.manager=" + empid +" )or (n.isByDept='N' and n.deptid=" + emp.getDeptid() + " and n.jobid=" + emp.getJobid() + "))");
        if (approval != null) {
            if (!"".equals(approval.getDocNo()) && approval.getDocNo() != null)
            {
                hql = hql+ " and a.docNo like '%" + approval.getDocNo() + "%'";
            }
            if (!"".equals(approval.getProposer()) && approval.getProposer() != null)
            {
                hql = hql+ " and a.proposer='" + approval.getProposer() + "'";
            }
    }
        hql = hql + " order by a.oid desc";
        return super.pageFenYE(hql, page, pageSize);
    }

    @Override
    public LcApproval getApproval(Integer oid) {
        return (LcApproval) selectByHQL("from LcApproval a where a.oid=" + oid);
    }

    public LcApproval approvalInfo(Integer oid) {

        String hql="from LcApproval a left join fetch a.emp where a.oid=" + oid;
        return (LcApproval) selectByHQL(hql);
    }

    public List<LcApproval> listApprovalByDocNo(String docNo) {
        String hql = "from LcApproval a left join fetch a.approvalEmp left join fetch a.dept left join fetch a.node n left join fetch n.dept left join fetch n.job where a.docNo='" +
                docNo + "'";
        return queryAll(hql);
    }

}
